
package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.app.Activity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.FeedController;
import com.alimama.mobile.sdk.config.FeedProperties;
import com.alimama.mobile.sdk.config.MMPromoter;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.xm9m.ibczy.mytest.R;

import java.util.List;

public class FeedActivity extends Activity implements FeedController.IncubatedListener,FeedController.LazyDataCallback{
    FeedController mController;
    MmuSDK mmuSDK;
    private String slotid = "63841";
    private String slotid_b = "63840";

    private FeedController.MMFeed useFeed;
    private String infos = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed);
        TextView tv = (TextView) findViewById(R.id.info);
        tv.setMovementMethod(ScrollingMovementMethod.getInstance());

        mmuSDK = MmuSDKFactory.getMmuSDK();
        mmuSDK.init(getApplication());//初始化SDK,该方法必须保证在集成代码前调用，可移到程序入口处调用
        mController = FeedProperties.getMmuController();//如果插件加载失败，将返回null，在使用controller请都先判空
        if(mController != null){
            mController.setIncubatedListener(this);//设置Feed初始化过程的监听
        }

    }

    /**
     * 初始化广告位
     * @param view
     */
    public void addSlots(View view) {
        FeedProperties prop = new FeedProperties(slotid);
        prop.tag = "Xuzhen";//传递额外参数

        mmuSDK.attach(prop);//attach以后将马上开始初始化广告位（异步不阻塞）
    }

    /**
     * 获取已初始化的Feed
     * @param view
     */
    public void getProduct(View view) {
        if(mController != null) {
            FeedController.MMFeed feed = mController.getProduct(slotid);//获取Feed，如果广告位未初始化或初始化失败将返回null
            mController.setLazyDataCallback(feed,this);
            feed.setObj("Object");
//            feed.setTag("Tag");

            print(feed, slotid);
            print(mController.getProduct(slotid_b),slotid_b);
            useFeed = feed;
        }else{
            addMesage("mController == null,可能插件加载失败。");
        }
    }

    /**
     * 点击一个已经获取的推广内容
     * @param view
     */
    public void clickPromoter(View view){
        if(useFeed != null) {
            if(useFeed != null && useFeed.isReady()){//检查推广内容完整性
                List<MMPromoter> promoters = useFeed.getPromoters();
                if(promoters != null && promoters.size() > 0){//最好判断一下
                    MMPromoter p = promoters.get(0);
                    useFeed.reportImpression(this,p);
                    useFeed.clickOnPromoter(this,p);
                    addMesage(String.format("Slot[%s]点击广告成功。",slotid));
                }else{
                    addMesage(String.format("Slot[%s]获取Promoter失败。",slotid));
                }

            }else{
                addMesage(String.format("Slot[%s]获取Feed失败，请确认是否已经初始化。",slotid));
            }

        }else{
            addMesage("useFeed == null，确认已经获取Feed。");
        }
    }

    private void print(FeedController.MMFeed f,String slotid) {
        if (f != null) {
            Log.i("Feeds", f.toString());
            addMesage(f.toString());
            List<MMPromoter> promoters = f.getPromoters();
            for (MMPromoter p : promoters) {
                Log.i("Feeds", p.toString());
                addMesage("    promoter:"+ p.getTitle());
            }
        }else{
            addMesage(String.format("Slot[%s]获取Feed失败，请确认是否已经初始化。",slotid));
        }
    }

    private void addMesage(String str){
        infos +="\r\n"+str;
        ((TextView)findViewById(R.id.info)).setText(infos);
    }

    @Override
    public void onComplete(int status, String slot) {
        try {
            if (0 == status) {
                Log.i("Feeds", String.format("推广位[%s]获取数据失败，将无法获取Feed.", slot));
                addMesage(String.format("推广位[%s]获取数据失败，将无法获取Feed.", slot));
            } else if (1 == status) {
                Log.i("Feeds", String.format("推广位[%s]的实时数据已经准备好", slot));
                addMesage(String.format("推广位[%s]的实时数据已经准备好", slot));
            } else if (2 == status) {
                Log.i("Feeds", String.format("推广位[%s]的缓存数据已经准备好", slot));
                addMesage(String.format("推广位[%s]的缓存数据已经准备好", slot));
            }
        } catch (Exception e) {
        }
    }

    @Override
    public void onClick() {
        Toast.makeText(this,"onClick",1).show();
    }

    @Override
    public void onReviced(FeedController.MMFeed feed) {
        feed.reflushData();
        addMesage("实时数据数据更新:");
        print(feed,feed.getSlotId());


    }
}
